﻿using System;
using System.Collections.Generic;

using System.Text;
using BGM.Sensors.MeasuringDevice;
using BGM.Domain.DTO;
using System.Globalization;
using System.IO;

namespace BayerContour
{
    public class BayerContourDevice : IMeasuringDevice
    {
        BayerCommController device;
        public BayerContourDevice()
        {
            device = new BayerCommController();
            device.MessageRecieved += new MessageRecievedEventhandler(device_MessageRecieved);
        }

        void device_MessageRecieved(object sender, MessageRecievedEventArgs e)
        {
            MessageRecievedEventhandler handler = MessageRecieved;
            if (handler != null)
                handler(this, e);
        }
        #region IMeasuringDevice Members

        public string Description
        {
            get { return "BAYER Contour"; }
        }

        public List<Reading> GetReadings(object o)
        {
            List<BayerContour.BayerCommController.Frame> data = null;
            if (o is string)
                data = device.Upload(o as string);
            else if (o is Stream)
                data = device.Upload(o as Stream);

            List<Reading> list = new List<Reading>();
            if (data != null)
                foreach (var item in data)
                {
                    var reading = ExstractDTO(item) as Reading;
                    if (reading != null)
                        list.Add(reading); ;
                }

            return list;
        }

        public event MessageRecievedEventhandler MessageRecieved;

        public string Name
        {
            get { return "BAYER Contour"; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            // throw new NotImplementedException();
        }

        #endregion


        private DateTime ExstractDateTime(string msgDate)
        {
            return DateTime.ParseExact(msgDate, "yyyyMMddHHmm", new CultureInfo(""));
        }
        private Reading ExstractDTO(BayerCommController.Frame frame)
        {
            var d = frame.data.Split('|');
            switch (d[0])
            {
                case "H":
                    //Header record
                    var senderId = d[4].Split('^');
                    //p = meter product code 
                    //v = meter software version\EEPROM version 
                    //s = meter serial number 
                    var deviceTime = ExstractDateTime(d[13]);
                    break;
                case "R":
                    if (d[2] == "^^^Glucose")
                    {
                        var value = double.Parse(d[3], new CultureInfo("EN-US").NumberFormat);
                        var unitlabelAndRefMethod = d[4].Split('^');
                        var fixedUnitLabel = unitlabelAndRefMethod[0];
                        var referenceMethod = unitlabelAndRefMethod[1];
                        var measuringDate = ExstractDateTime(d[11]);
                        var valueInMgPrDl = (fixedUnitLabel == "mg/dL" ? value : value * 18);
                        return new Reading(measuringDate, valueInMgPrDl);//warning measurment is rounded the reading should be changed to use doule values instead.

                    }
                    break;
                default:

                    break;
            }
            return null;

        }
    }
}
